<?php
/**
 * Redundant class. Not yet used 
 */ 
class Clu_Controller_Action_Widget
{
	public $widget; //contains all the info about the widget in a specific lang & theme & template...
	public $requestParams; //
	
	public $viewParams;
	public $widgetService;
	 
    public function __construct($widget, $params) //just a simple array
    {
    	$this->widget = $widget;
        $this->requestParams = $params;
        $this->viewParams = null;
        $this->init();  
    }
    
    public function init()
    {
        $this->db = Zend_Db_Table::getDefaultAdapter(); //TODO: Remove this when the Models are fine
		$this->widgetService = new Clu_Service_Widget();
    }
	
    public function getParam($key)
    {
    	if (isset($this->requestParams[$key]))
    		return $this->requestParams[$key];
    	else
    		die("Class <b>" . get_class($this) ." </b>doest not have param '<i>" . $key ."</i>' passed to it. Check your widget_position configuration");
    }
    
    public function issetParam($key)
    {
    	if (isset($this->requestParams[$key]))
    		return true;
    	else
    		return false;
    }
        
    public function renderToHolder ()
    {
    	//if ($this->widget['loading_type'] == 0) //normal
    	//if (!isset($this->widget['clu_no_layout'])
    	{
        	$tmp = explode('/', $this->widget['widget_request']);
        	$script = '/modules/' . $tmp[0] . '/views/scripts/' . $this->widget['script_path'] . ".phtml";
        	$region =  $this->widget['region'];
        	$weight =  $this->widget['weight'];
//        	die_a($this->viewParams);
        	if ($this->widgetService->renderToHolder($this->viewParams, $script, $region, $weight) == false)
        		die("Class: " . get_class($this) . ": region $region and weight $weight already exist");
    	}
    	//else //no layout, just view and return
    	{
			
    	}
    	//TODO unset($this->widgetService);
    }
    
    public function renderNoHolderAndReturn()
    {
   		$tmp = explode('/', $this->widget['widget_request']);
       	$script = '/modules/' . $tmp[0] . '/views/scripts/' . $this->widget['script_path'] . ".phtml";
       	//echo get_class($this->widgetService->view);//->render($script);
       	$widgetService = $this->widgetService;
       	$view = $widgetService->getView();
       	$view->viewParams = $this->viewParams;
       	echo $view->render($script);
       	exit();
    }
    
    public function setHolder ($content, $regionId = null, $weight = null )
    {
    }
    
    protected function addMiniAction($miniRequest)
    {
    }    
    
    public function __call($method, $args)
    {
    	Zend_Registry::get('logger')->log("Action: " . $method, Zend_Log::INFO);
    	die($method . ' does not exists in class ' . get_class($this));
        return;
        // TODO: in deployment mode, forward to an error page
        if ('Action' == substr($method, -6)) {
            // If the action method was not found, render the error
            // template
            //return $this->render('error');
            die ("action <b>" . $method . "</b> for module <b>". $this->getRequest()->getModuleName() ."</b> does not exist <br/>");
        }

        // all other methods throw an exception
        throw new Exception('Invalid method "'
                            . $method
                            . '" called',
                            500);
    }

	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setItemCountPerPage($itemsPerPage);
        $paginator->setCurrentPageNumber($pageNumber);
        $paginator->setPageRange($pageRange);
        Zend_Paginator::setDefaultScrollingStyle($style);
        return $paginator;
    }
    
}
?>